#!/usr/bin/python

from __future__ import print_function
import copy
from classtools import AttrDisplay

class World(AttrDisplay):
    
    def namePlace(self, (latitude, longitude), name):
        self.world[latitude][longitude][0] = name
    def setPlaceEnv(self, (latitude, longitude), environment):
        self.world[latitude][longitude][1] = environment
    def addToPlace(self, (latitude, longitude), object):
        self.world[latitude][longitude][2].update({id(object):object})
    def removeFromPlace(self, (latitude, longitude), object):
        del self.world[latitude][longitude][2][id(object)]
    def moveFromToPlace(self, (fromlat, fromlong),(tolat, tolong), object):
        if (tolat < 0 or tolat > self.worldSize):
            return
        if (tolong < 0 or tolong > self.worldSize):
            return
        del self.world[fromlat][fromlong][2][id(object)]
        self.world[tolat][tolong][2].update({id(object):object})
        object.position = (tolat, tolong)
    def printMap(self):
        print('+' + '--' * self.worldSize + '+')
        for latitude in range(self.worldSize):
            print('|', end='')
            for longitude in range(self.worldSize):
                print(self.world[latitude][longitude][1][0] + ' ', end='')
                for object in self.world[latitude][longitude][2].values():
                    print(object.name[0], end='')
            print('|')
        print('+' + '--' * self.worldSize + '+')

    def __init__(self, worldSize=2):
        self.worldSize = worldSize
        place = ['', '    ', {}]
        self.world = [[copy.deepcopy(place) for x in range(worldSize)]
                                              for y in range(worldSize)]
    
    terrain = {
                ' ': 'Unknown',
                '_': 'Grass Land',
                '^': 'Mountains',
                '"': 'Hills',
                '%': 'Forest',
                '#': 'Walled City',
                '*': 'Cultivated Land',
                '~': 'River / Lake',
                '-': 'Marshes',
                '.': 'Desert',
              }

    weather = {
                ' ': 'Unknown',
                'w': 'Wet',
                'd': 'Dry',
                'h': 'Humid',
                'l': 'Cold',
                'r': 'Hot',
                't': 'Temperate',
              }
    mobility = {
                ' ': 'Unknown',
                'R': 'Road',
                'T': 'Track',
                'C': 'Canal',
               }

    direction = {
                ' ': 'Unknown',
                '0': 'Center',
                '1': 'North',
                '2': 'North East',
                '3': 'East',
                '4': 'South East',
                '5': 'South',
                '6': 'South West',
                '7': 'West',
                '8': 'North West',
               }

    def showPlace(self, latitude, longitude):
        print(self.world[latitude][longitude])
        print('%s, %s, %s, %s' % (
                self.terrain[self.world[latitude][longitude][1][0]],
                self.weather[self.world[latitude][longitude][1][1]],
                self.mobility[self.world[latitude][longitude][1][2]],
                self.direction[self.world[latitude][longitude][1][3]]
                ))
        for object in self.world[latitude][longitude][2].values():
            print(object)


if __name__ == '__main__':
    myworld = World()
    myworld.namePlace(0, 0, 'Luo Yang')
    myworld.setPlaceEnv(0, 0, '%tR0')
    myworld.namePlace(0, 1, 'Yang Zhou')
    myworld.setPlaceEnv(0, 1, '-tC0')
    myworld.namePlace(1, 0, 'Cheng Du')
    myworld.setPlaceEnv(1, 0, '^tR0')
    myworld.namePlace(1, 1, 'Chang An')
    myworld.setPlaceEnv(1, 1, '#tR0')
    myworld.showPlace(0, 0)
    myworld.showPlace(0, 1)
    myworld.showPlace(1, 0)
    myworld.showPlace(1, 1)
